package com.neusoft.springmvc.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartResolver;

import com.neusoft.springmvc.po.Emp;
import com.neusoft.springmvc.po.User;
import com.neusoft.springmvc.service.HelloService;

//@Controller
//@ResponseBody //将数据放入响应体中，以响应流形式带给前端，相当于之前的response.getWriter
@RestController
@RequestMapping("/hello")
public class HelloController {
	
//	@RequestMapping("/hello")
////	@GetMapping("/hello")
////	@PostMapping("/hello")
//	public String hello(HttpServletRequest request,HttpServletResponse response){
//		String name = request.getParameter("name");
//		return name+"hello world";//此处会默认调用jackson jon解析器
//	}
	
	@Autowired
	private HelloService helloService;
	
	@RequestMapping("/hello")
	public String hello(@RequestParam("aaa")String username){
		return username+" hello world";//此处会默认调用jackson jon解析器
	}
	
	@RequestMapping("/do_login")
	public boolean login(User u){
		
		return helloService.login(u);
	}
	
	@RequestMapping("/upload")
	public boolean upload(MultipartFile file){
		//定义目标存储地址
		String targetPath = "F:/1111";
		//处理文件名字（定义新文件名字）
		//需要先获取旧文件名，重点是后缀
		String[] split = file.getOriginalFilename().split("\\.");
		String oldsuf = split[split.length-1];
		String newFileName = UUID.randomUUID().toString()+"."+oldsuf;
		//构建文件对象
		File newFile = new File(targetPath,newFileName);
		//判断目录是否存在
		if (!newFile.exists()) {
			newFile.mkdirs();
		}
		//将用户文件内容传输到新文件对象中
		try {
			file.transferTo(newFile);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} 
		
		return true;
	}
	


}
